package no.ntnu.fp.model;

import no.ntnu.fp.utils.XmlParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Notice extends Entity {
	private Employee employee;
	private Meeting meeting;
	private NoticeType noticeType;
	private String description;
	private boolean isRead;

	public Employee getEmployee() {
		return employee;
	}

	public String getDescription(){
		return description;
	}

	public Meeting getMeeting() {
		return meeting;
	}

	public NoticeType getNoticeType() {
		return noticeType;
	}

	public boolean isRead() {
		return isRead;
	}

	/**
	 * 
	 * @author H�kon Heggedal
	 * Hva slags varsel skal dukke opp i gui?
	 *  nytt m�te, m�tet er endret, m�tet er avlyst,
	 *   deltager godtar/deltaker avsl�r m�teinnkalling
	 */
	public enum NoticeType {
		NewMeeting, ChangedMeeting, CancelledMeeting, ParticipantDeclined, ParticipantAccepted
	}
	
	
	
	public Notice (Element root) {
		super(root);
	}
	public Notice(long id, Employee employee, Meeting meeting, 
			NoticeType type, String description, boolean isRead){
		super(id);
		this.employee = employee;
		this.meeting = meeting;
		this.noticeType = type;
		this.description = description;
		this.isRead = isRead;
	}

	@Override
	protected void initializeFromXml(Element rootNode) {
		this.id = Long.parseLong(XmlParser.getTextValue(rootNode, "id"));
		this.employee = new Employee((Element)rootNode.getElementsByTagName("employee").item(0));
		this.meeting = new Meeting((Element)rootNode.getElementsByTagName("meeting").item(0));
		this.description = XmlParser.getTextValue(rootNode, "description");
		this.noticeType = NoticeType.valueOf(XmlParser.getTextValue(rootNode, "noticeType"));
		this.isRead = Boolean.parseBoolean(XmlParser.getTextValue(rootNode, "isRead"));
	}

	@Override
	public Element toXml(Document dom) {
		Element rootElement = dom.createElement("notice");
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "id", id + ""));
		rootElement.appendChild(employee.toXml(dom));
		rootElement.appendChild(this.meeting.toXml(dom));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "description", description));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "noticeType", noticeType + ""));
		rootElement.appendChild(XmlParser.createElementWithValue(dom, "isRead", isRead + ""));
		return rootElement;
	}

	@Override
	public String toString() {
		return super.toString() + " Description: " + getDescription();
	}

}
